package com.xul.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xul.yygh.hosp.repository.DepartmentRepository;
import com.xul.yygh.hosp.service.DepartmentService;
import model.com.xul.yygh.model.hosp.Department;
import model.com.xul.yygh.vo.hosp.DepartmentQueryVo;
import model.com.xul.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author xul
 * @create 2021-07-11 15:40
 */

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentRepository departmentRepository;

    /**
     * 上传科室
     *
     * @param paramMap
     */
    @Override
    public void save(Map<String, Object> paramMap) {
        String toJSONString = JSONObject.toJSONString(paramMap);
        Department department = JSONObject.parseObject(toJSONString, Department.class);
        Department departmentExist = departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());
        if (departmentExist != null) {
            departmentExist.setIsDeleted(0);
            departmentExist.setUpdateTime(new Date());
            departmentRepository.save(departmentExist);
        } else {
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }
    }

    /**
     * 查询科室信息
     *
     * @param page              当前页
     * @param limit             每页记录数
     * @param departmentQueryVo
     * @return
     */
    @Override
    public Page<Department> findPageDepartment(int page, int limit, DepartmentQueryVo departmentQueryVo) {
        // 创建Pageable对象 设置当前页 和每页记录数
        // 在mongodb中使用pageable中的page是从第0页开始计算的
        Pageable pageable = PageRequest.of(page - 1, limit);
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo, department);
        department.setIsDeleted(0);
        // 创建example对象
        ExampleMatcher matcher = ExampleMatcher.matching().withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING).withIgnoreCase(true);
        Example<Department> example = Example.of(department, matcher);
        Page<Department> departments = departmentRepository.findAll(example, pageable);
        return departments;
    }

    /**
     * 删除科室信息
     *
     * @param hoscode
     * @param depcode
     */
    @Override
    public void remove(String hoscode, String depcode) {
        // 根据医院编号 科室编号查询科室信息
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (department != null) {
            // 调用删除方法
            departmentRepository.deleteById(department.getId());
        }
    }

    /**
     * 根据医院编号,查询所有科室列表
     *
     * @param hoscode
     * @return
     */
    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        //创建list集合，用于最终数据封装
        List<DepartmentVo> result = new ArrayList<>();
        // 根据医院编号, 查询医院的所有信息
        Department department = new Department();
        department.setHoscode(hoscode);
        Example<Department> example = Example.of(department);
        // 查询到所有的科室列表
        List<Department> departmentList = departmentRepository.findAll(example);
        // 根据大科室bigCode经行分组,获取大科室下面的子科室
        Map<String, List<Department>> collect = departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));
        // 遍历分组好的集合
        for (Map.Entry<String, List<Department>> entry : collect.entrySet()) {
            // 集合的key就是大科室的bigCode
            String bigCode = entry.getKey();
            // value就是大科室所对应的数据
            List<Department> value = entry.getValue();
            // 封装大科室
            DepartmentVo departmentVo = new DepartmentVo();
            departmentVo.setDepcode(bigCode);
            departmentVo.setDepname(value.get(0).getBigname());
            // 封装小科室
            List<DepartmentVo> children = new ArrayList<>();
            for (Department department1 : value) {
                DepartmentVo departmentVo1 = new DepartmentVo();
                departmentVo1.setDepcode(department1.getDepcode());
                departmentVo1.setDepname(department1.getDepname());
                // 封装到list集合中
                children.add(departmentVo1);
            }
            // 把小科室的list放到大科室的children中
            departmentVo.setChildren(children);
            // 最终放到result中
            result.add(departmentVo);
        }
        return result;
    }

    /**
     * 根据医院编号和科室编号查询科室名称
     *
     * @param hoscode
     * @param depcode
     * @return
     */
    @Override
    public String getDepName(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (department != null) {
            return department.getDepname();
        } else {
            return null;
        }
    }

    /**
     * 根据医院编号和科室编号查询科室信息
     *
     * @param hoscode
     * @param depcode
     * @return
     */
    @Override
    public Department getDepartment(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        return department;
    }

}
